package com.example.wallpaperapp.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.wallpaperapp.R;
import com.example.wallpaperapp.bean.HomeChoicBean;
import com.example.wallpaperapp.widget.RoundImageView2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created on 2019/5/17 10:39
 *
 * @author Q.PeterGuo
 * @version 1.0.0
 * @Description
 */
public class HomeChoicAdapter extends RecyclerView.Adapter<HomeChoicAdapter.ViewHolder> {
    private Context mContext;
    private List<HomeChoicBean> dataList;
    private int random;
    private List<Integer> heightList = new ArrayList<Integer>();
    private MyOnItemClickListener myOnItemClcikListener;
    private MyOnLongClickListener myOnLongClickListener;

    public HomeChoicAdapter(Context context, List<HomeChoicBean> data) {
        mContext = context;
        dataList = data;
        //初始化高度
        if (dataList != null && dataList.size() != 0) {
            for (int i = 0; i < dataList.size(); i++) {
                //随机数公式
                // int s = random.nextInt(max)%(max-min+1) + min;
                random = new Random().nextInt(600) % (201) + 400;//[100,300)
                heightList.add(random);
            }
        }
    }

    public void setData(List<HomeChoicBean> data) {
        dataList = data;
        setImageScale();
    }

    private void setImageScale() {
        for (final HomeChoicBean girlBean : dataList) {
            if (girlBean.getScale() == 0) {
                Glide.with(mContext).load(girlBean.getUrl()).into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        float scale = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight();
                        girlBean.setScale(scale);
                        notifyDataSetChanged();
                    }
                });
            } else {
                notifyDataSetChanged();
            }
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_home_cho, null);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HomeChoicBean result = dataList.get(position);
        int height = heightList.get(position);
        //得到控件的高度
        ViewGroup.LayoutParams layoutParams = holder.mIv.getLayoutParams();
        //设置高度
        layoutParams.height = height;
        //设置点击事件
        if (myOnItemClcikListener != null && !holder.mIv.hasOnClickListeners()) {
            holder.mIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //回调方法,,将view控件和索引传回,索引必须是布局在父布局中的索引,否则索引错乱最终报错
                    myOnItemClcikListener.myOnItemClickListener(holder.getLayoutPosition(), v);
                }
            });
        }
        if (myOnLongClickListener != null) {
            holder.mIv.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    myOnLongClickListener.myOnLongClickListener(holder.getLayoutPosition(), v);
                    return true;
                }
            });
        }
        Glide.with(mContext).load(result.getUrl()).into(holder.mIv);

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final RoundImageView2 mIv;

        public ViewHolder(View itemView) {
            super(itemView);
            mIv = itemView.findViewById(R.id.iv);
        }
    }

    //对外暴露的方法,当设置该方法时,会创建接口的实现类
    public void setOnItemClickListener(MyOnItemClickListener myOnItemClickListener) {
        this.myOnItemClcikListener = myOnItemClickListener;
    }

    public void setOnLongCickListener(MyOnLongClickListener myOnLongClickListener) {
        this.myOnLongClickListener = myOnLongClickListener;
    }

    //自定义点击接口
    public interface MyOnItemClickListener {
        void myOnItemClickListener(int position, View view);
    }

    //自定义长按接口
    public interface MyOnLongClickListener {
        void myOnLongClickListener(int position, View view);
    }
}
